package cn.zhaoguang.usedcar.main.config;

import cn.zhaoguang.usedcar.main.interceptor.AuthenticationInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @Description
 * @Author zhaoguang
 * @Date 2021/3/13 2:12
 */
@Configuration
public class CustomWebMvcConfig implements WebMvcConfigurer {

    /* 拦截器加载是在springcontext创建之前完成的，所以需要提前加载 */
    @Bean
    public HandlerInterceptor getAuthenticationInterceptor() {
        return new AuthenticationInterceptor();
    }

    /* 配置静态资源，访问时在URL加上 /static/ */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/car/img/**").addResourceLocations("file:F:/Project/usedcar/car-img/");

        //用户端
        registry.addResourceHandler("/client/**").addResourceLocations("classpath:/vue/client/");
        //商家端
        registry.addResourceHandler("/seller/**").addResourceLocations("classpath:/vue/seller/");
        //后台管理员端
        registry.addResourceHandler("/manage/**").addResourceLocations("classpath:/vue/manage/");
    }

    /* 配置全局跨域 */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedHeaders("*")
                .allowedMethods("*")
                .maxAge(1800);
    }

    /* 配置自定义拦截器 */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getAuthenticationInterceptor())
                .addPathPatterns("/manage/add")
                .excludePathPatterns("/manage/login");
    }

    /* 设置无业务逻辑页面跳转 */
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/client").setViewName("redirect:/client/index.html");
        registry.addViewController("/seller").setViewName("redirect:/seller/index.html");
        registry.addViewController("/manage").setViewName("redirect:/manage/index.html");
    }

}
